<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/device' }"
          >设备管理
        </el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/device/bind' }"
          >库存设备
        </el-breadcrumb-item>
        <el-breadcrumb-item>指令列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix">
      <el-form class="pull-right" inline>
        <el-form-item>
          <el-button-group>
            <el-button
              @click="instructDialogVisible = true"
              size="medium"
              type="primary"
              >发送指令
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="box-card" shadow="never">
      <el-table :data="table_data" stripe style="width: 100%">
        <el-table-column
          label="状态"
          prop="instructResult"
          show-overflow-tooltip
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              disable-transitions
              type="warning"
              v-if="scope.row.instructResult === 0"
            >
              待响应
            </el-tag>
            <el-tag
              disable-transitions
              type="success"
              v-else-if="scope.row.instructResult === 1"
            >
              成功
            </el-tag>
            <el-tag disable-transitions type="danger" v-else>
              失败
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="命令"
          prop="instructContent"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column label="发送时间" prop="ctime" show-overflow-tooltip />
        <el-table-column label="送达时间" prop="utime" show-overflow-tooltip />
        <el-table-column
          label="操作用户"
          prop="userAccount"
          show-overflow-tooltip
        />
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size.sync="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>

    <el-dialog
      :visible.sync="instructDialogVisible"
      @close="dialogClose"
      title="发送指令"
      width="50%"
    >
      <el-form
        :model="instructForm"
        :rules="rules"
        label-width="100px"
        ref="instructForm"
        style="max-width: 400px"
      >
        <el-form-item label="指令代码" prop="instructContent">
          <el-input autocomplete="off" v-model="instructForm.instructContent" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="handleInstructSubmit('instructForm')" type="primary"
          >确 定
        </el-button>
        <el-button @click="instructDialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      id: null,
      instructDialogVisible: false,
      instructForm: {
        instructContent: null
      },
      rules: {
        instructContent: [
          { required: true, message: "请输入指令内容", trigger: "none" }
        ]
      }
    };
  },
  computed: {
    ...mapState("device_instruct", ["total_size", "table_data"]),
    page_size: {
      get() {
        return this.$store.state.device_instruct.page_size;
      },
      set(value) {
        this.set_page_size(value);
      }
    },
    current_page: {
      get() {
        return this.$store.state.device_instruct.current_page;
      },
      set(value) {
        this.set_current_page(value);
      }
    },
    search_param: {
      get() {
        return this.$store.state.device_instruct.search_param;
      }
    }
  },
  mounted() {
    this.id = this.$route.params.id;
    this.set_search_param({ deviceBaseId: this.id });
    this.load_table_data();
  },
  methods: {
    ...mapActions("device_instruct", [
      "load_table_data",
      "set_page_size",
      "set_current_page",
      "set_search_param"
    ]),
    handleInstructSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.device_instruct
            .send({
              deviceBaseId: this.id,
              instructContent: this.instructForm.instructContent
            })
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "指令发送成功!"
                });
                this.instructDialogVisible = false;
                this.load_table_data();
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
